define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cast/item/index' + location.search,
                    add_url: 'cast/item/add',
                    edit_url: 'cast/item/edit',
                    del_url: 'cast/item/del',
                    multi_url: 'cast/item/multi',
                    table: 'self_cast_item',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'cate_name', title: '分类名称', operate: false},

                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'href', title: __('Href'), formatter: Table.api.formatter.url, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,

                            buttons: [

                                {
                                    name: 'tocatch',
                                    text: '抓取',
                                    title: '抓取',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-hand-o-up',
                                    hidden: function(row) {
                                        console.log($.inArray(row.tag, Config.tags))
                                        if ($.inArray(row.tag, Config.tags) < 0) {
                                            return true
                                        } else {
                                            return false;
                                        }
                                    },
                                    click: function (event, data) {
                                        Fast.api.ajax({
                                            url: 'cast.item/catch',
                                            data: {
                                                tag: data.tag
                                            }
                                        }, function (ret) {
                                        });
                                    }

                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'cast/item/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'cast/item/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'cast/item/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                function show(value) {
                    if (value == 0) {
                        $('#type_1').hide();
                        $('#type_0').show();
                    } else {
                        $('#type_1').show();
                        $('#type_0').hide();
                    }
                }
                var value = $('#c-type').val();
                show(value);
                $('#c-type').change(function (e) {
                    var value = $(this).val();
                    show(value);
                });


                Form.api.bindevent($("form[role=form]"));


            },
            css: {
                url: function () {
                    return {
                        css: {
                            'max-width': '350px',
                            'word-wrap': 'break-word',
                            'word-break': 'normal',
                            'max-height': '150px',
                            'display':'inline-block',
                            'overflow': 'hidden'
                        }
                    };
                }
            }
        }
    };
    return Controller;
});